package tool.analysis.series;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import tool.csv.CSVCompatible;
import tool.csv.FileWriter;

    /**
     * @author jwb09119
     * @date 2013/12/17
     * 
     * This analyser records the ratios of various qualities of experiments such as proportion of
     * classes, abstract classes and interfaces.  It provides a high-level view of a series of
     * experiments.
     * 
     * Related GQM: A1, A3, A4
     */

public class EntityRatioAnalysis extends CSVCompatible implements ISeriesAnalysis{

    private List<EntitiesRecord> records;
    
    public EntityRatioAnalysis(FileWriter writer) {

        super(writer);
        records = new ArrayList<>();
        
    }

    
    @Override
    public String getAnalysisTag () {

        return "Entity Ratios";
        
    }
    
    
    @Override
    public void analyseExperiments (Collection<ISeriesMember> experiments) {

        for (ISeriesMember sm : experiments) {
            
            EntitiesRecord er = new EntitiesRecord(sm.getSubjectName(),
                                                   sm.getConcreteClassCount(),
                                                   sm.getAbstractClassCount(),
                                                   sm.getInterfaceCount());
            
            records.add(er);
            
        }
        
    }

    
    /**
     * An inner class that is just a 4-tuple for information about one of the experiments in the
     * analysed series, and is not intended for use outside this class.
     */
    private class EntitiesRecord {
        
        private final String name;
        private final int concreteClasses;
        private final int abstractClasses;
        private final int interfaces;
        private final double percentInterfaces;
        
        private EntitiesRecord (String name, int cc, int ac, int i) {
            
            this.name = name;
            concreteClasses = cc;
            abstractClasses = ac;
            interfaces = i;
            BigDecimal bd = new BigDecimal (((float)interfaces)/
                                ((float)(concreteClasses+abstractClasses+interfaces)) * 100);
            BigDecimal rounded = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
            percentInterfaces = rounded.doubleValue();
            
        }
        
        
        @Override
        public String toString () {
            
            StringBuilder reply = new StringBuilder();
            
            reply.append(name);
            reply.append(", ");
            reply.append(concreteClasses);
            reply.append(", ");
            reply.append(abstractClasses);
            reply.append(", ");
            reply.append(interfaces);
            reply.append(", ");
            reply.append(percentInterfaces);
            reply.append("%");
            
            return reply.toString();
            
        }
        
    }
    
    
    /**********************
     * CSV Export Methods *
     **********************/
    
    @Override
    protected String getExportHeader () {

        return "Summary counts and ratios for entity types in this experimental series";
        
    }

    
    @Override
    protected void writeStateToCSV (FileWriter writer) {

        writer.writeComment("SubjectName, CC, AC, I, %I");
        
        for (EntitiesRecord e : records) {
            writer.writeComment(e.name, 
                                String.valueOf(e.concreteClasses), 
                                String.valueOf(e.abstractClasses), 
                                String.valueOf(e.interfaces), 
                                String.valueOf(e.percentInterfaces));
        }
        
    }





}
